package xyz.chaobei.thread.blocked;

import java.util.LinkedList;
import java.util.List;

/**
 * @author <a href='mailto:maruichao52@gmail.com'>MRC</a>
 * @since 2021/1/29
 */
public class FileList {

    private List<String> list = new LinkedList<>();
    private int index = 0;

    public FileList() {
        list.add("FILE01");
        list.add("FILE02");
        list.add("FILE03");
    }

    /**
     * <p>每次获取一个文件
     * <p>author: <a href='mailto:maruichao52@gmail.com'>MRC</a>
     *
     * @return void
     * @since 2021/1/29
     **/
    public synchronized void getFile() {

        System.out.println(Thread.currentThread().getName() + "尝试获取文件");

        if (index >= list.size()) {
            System.out.println(Thread.currentThread().getName() + "来迟了~文件没有了");
            return;
        }
        String name = list.get(index);
        index++;
        System.out.println(Thread.currentThread().getName() + "成功获取文件：" + name);
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
